﻿using System;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.IDAL;
using OgilvyOne.Model;
using System.Data;
using System.Data.SqlClient;

namespace OgilvyOne.SqlServerDAL
{
    public class TypeManage : TypeManageIDAL
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;

        public System.Data.DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select COLUMNTYPEID,CTNAME,BACKLINK ");
            strSql.Append(" FROM CMS_COLUMN_TYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by COLUMNTYPEID desc");
            return SQLHelper.Query(strSql.ToString());
        }

        public bool Delete(int COLUMNTYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CMS_COLUMN_TYPE ");
            strSql.Append("where COLUMNTYPEID = @COLUMNTYPEID");
            SqlParameter[] parameters = {
					new SqlParameter("@COLUMNTYPEID", SqlDbType.Int)
             };
            parameters[0].Value = COLUMNTYPEID;
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        #region TypeManageIDAL 成员

        public bool Exists(string columntypeid)
        {
            throw new NotImplementedException();
        }

        public int Add(TypeManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CMS_COLUMN_TYPE (CTNAME,BACKLINK,ADDTIME,CLASSNAME,ADDMANID,EXPLAIN)");
            strSql.Append(" values (@CTNAME,@BACKLINK,@ADDTIME,@CLASSNAME,@ADDMANID,@EXPLAIN)");
            SqlParameter[] parameters = {
                    new SqlParameter("@CTNAME",SqlDbType.VarChar,50),
                    new SqlParameter("@BACKLINK",SqlDbType.VarChar,255),
                    new SqlParameter("@CLASSNAME",SqlDbType.VarChar,255),
                    new SqlParameter("@ADDTIME",SqlDbType.DateTime),
                    new SqlParameter("@ADDMANID",SqlDbType.VarChar,50),
                    new SqlParameter("@EXPLAIN",SqlDbType.VarChar,1000),
                                        };
            parameters[0].Value = model.Ctname;
            parameters[1].Value = model.Backlink;
            parameters[2].Value = model.Classname;
            parameters[3].Value = model.Addtime;
            parameters[4].Value = model.Addmanid;
            parameters[5].Value = model.Explain;
           

            int ts = 1;
            return ts = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        private const string COLUMN_SQL = "select * from CMS_COLUMN_TYPE where COLUMNTYPEID=@COLUMNTYPEID";
        public TypeManageModel selectType(int tpid)
        {
            TypeManageModel tmm = new TypeManageModel();
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@COLUMNTYPEID", SqlDbType.Int)
            };
            parms[0].Value = tpid;
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_SQL, parms))
            {
                if (rdr.Read())
                {
                    tmm.Columntypeid = rdr.GetInt32(0);
                    tmm.Ctname = rdr.GetString(1);
                    tmm.Backlink = rdr.GetString(2);
                    tmm.Classname = rdr.GetString(3);
                    tmm.Addtime = rdr.GetDateTime(4);
                    tmm.Addmanid = rdr.GetString(5);
                    tmm.Explain = rdr.GetString(6);

                }
            }
            return tmm;
        }


        public int updateType(TypeManageModel tmm)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE CMS_COLUMN_TYPE SET CTNAME=@CTNAME,BACKLINK=@BACKLINK,EXPLAIN=@EXPLAIN WHERE COLUMNTYPEID = @COLUMNTYPEID");
            SqlParameter[] parameters = {
                                            new SqlParameter("@CTNAME",SqlDbType.VarChar,255),
                                            new SqlParameter("@BACKLINK",SqlDbType.VarChar,255),
                                            new SqlParameter("@EXPLAIN",SqlDbType.VarChar,1000),
                                            new SqlParameter("@COLUMNTYPEID",SqlDbType.Int) 
                                        };

            parameters[0].Value = tmm.Ctname;
            parameters[1].Value = tmm.Backlink;
            parameters[2].Value = tmm.Explain;
            parameters[3].Value = tmm.Columntypeid;
            int ts = 1;
            return ts = SQLHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        #endregion
    }
}
